<?php

$path = str_replace('\\', '/', dirname(__FILE__)) . "/";
define('PHP_WX',$path."/php_wx/");
require_once("../vendor/autoload.php");
require_once("../lib/fun.php");
require_once("../lib/pdo.php");

//添加微信的配置
require_once PHP_WX."/lib/WxPay.Api.php";
require_once PHP_WX."example/WxPay.NativePay.php";
require_once PHP_WX.'example/log.php';


//初始化日志
$logHandler= new CLogFileHandler("../log.txt");
$log = Log::Init($logHandler, 15);
//微信支付参数
$wx_config = require("./config.php");

define('WX_CONFIG',$wx_config);


//vip数据
$db_vip = db('lu_vip');

$_vip = $db_vip->select();
$param = I("param.");
$act = I("param.act",'wx_back');
//小程序支付
if($param['act'] == 'web'){
    //获取用户d
    
    $rule  = [
        'id'  => [
            'type'         => 'number',
            'is_necessary' => 1,
            'name' => '订单ID',
        ],
        'act'  => [
            'type'         => 'string',
            'is_necessary' => 1,
            'name' => 'act',
        ],
         'token'  => [
            'type'         => 'string',
            'is_necessary' => 1,
            'name' => 'token',
        ],
    ];
    $check = CheckParams($param,$rule);
    if(!$check['success']){
        $msg = explode(";",$check['msg']);
        json([
            'code' => 0,
            'msg'  => $msg[0] ?? "",
        ]);
    }

    $db_user = db('lu_user');
    $token = str_replace(" ","+",I("token"));
    $uid = authcode($token,'DECODE',"youluda",7*24*3600);
    $user_info = $db_user->where(['id' => $uid])->find();
    if(empty($user_info)){
         json([
            'code' => 0,
            'msg' => '用户信息失败',
        ]);
    }
    $db = db("lu_order");
    $vip_data = $_vip;
    $vip_data = returnArrByKey($_vip,'id');
    if(!isset($vip_data[$param['id']])){
        json([
            'code' => 0,
            'msg'  => '参数错误',
        ]);
    }
    $vip_data = $vip_data[$param['id']];
    $order_no =  makeOrderNum();
    // $vip_data['money'] = 0.01;
    $add_order = [
            'vip_id' => $param['id'],
            'order_sn' => $order_no,
            'price' => $vip_data['money'],
            'type' => 1,
            'create_time' => time(),
            'uid' => $user_info['id'],
            ];
    $id = $db->add($add_order);
    if($id){
        $notify = new NativePay();
        $input = new WxPayUnifiedOrder();
        $input->SetBody("用户充值");
        $input->SetAttach("用户充值");
        $input->SetOut_trade_no($order_no);
        $input->SetTotal_fee($vip_data['money'] * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("vip");
        $input->SetNotify_url("https://www.uluda.cn/pay/wx_web_back.php");
        $input->SetTrade_type("NATIVE");
        $input->SetProduct_id($id);
        
        $result = $notify->GetPayUrl($input);
        $url2 = $result["code_url"];
        json([
                'code' => 1,
                'url' => $url2,
            ]);
        
    }else{
        json([
            'code' => 0,
            'msg'  => '提交失败',
        ]);
    }
    
}
//小程序回写
if($act == 'wx_back'){
    $pay = Pay::wechat($wx_config);
    file_put_contents("./wx.txt","<---------------------------------------------------------------------->".date("Y-m-d H:i:s").'<---->',FILE_APPEND);
    $data = $pay->verify();
    $info = $data->all();
    $info_arr = json_decode(json_encode($info), true);
    file_put_contents("./wx.txt",json_encode($info_arr),FILE_APPEND);
    $db = db('lu_order');
    $where = [
            'order_sn' => $info_arr['out_trade_no'],
        ];
    $order_info = $db->where($where)->find();


}

?>